//
//  VisitorLoginView.swift
//  SinaWeibo
//
//  Created by 梁楠 on 16/1/11.
//  Copyright © 2016年 QQ主流框架. All rights reserved.
//

import UIKit
import SnapKit

//声明可选的协议方法,需要在声明前加上@objc
@objc protocol VisitorLoginViewDelegate: NSObjectProtocol {
    
    optional func userWillRegister()
    
    func userWillLogin()
}

class VisitorLoginView: UIView {
    
    //声明代理,swift属性,默认strong
    //声明弱引用的属性用weak
    weak var visitorViewDelegate: VisitorLoginViewDelegate?
    
    //MARK: 按钮的监听事件方法
    @objc private func loginBtnDidClick() {
        
        // visitorViewDelegate? 相当于OC中判断 if (delegate != nil) { .... }
        visitorViewDelegate?.userWillLogin()
    }
    
    @objc private func registerBtnDidClick() {
        
        // userWillRegister? 相当于OC中判断 if ([delegate respondsToSelector()]) { ........}
        visitorViewDelegate?.userWillRegister?()
    }
    
    func setupUIWithInfo (imageName: String? , tipText: String) {

        if let img = imageName {
            
            circleIcon.image = UIImage(named: img)
            largeHouse.hidden = true
            bringSubviewToFront(circleIcon)
        } else {
            starAnimation()
        }
        tipLabel.text = tipText
    }
    
    func starAnimation () {
        
        //基本动画
        let base = CABasicAnimation(keyPath: "transform.rotation")
        
        base.repeatCount = MAXFLOAT
        base.toValue = 2 * M_PI
        
        //持续时间
        base.duration = 10
        
        //设置动画完成 / 页面失去活跃状态  不移除图层
        base.removedOnCompletion = false
        
        circleIcon.layer.addAnimation(base, forKey: nil)
        
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI () {
        addSubview(circleIcon)
        addSubview(backView)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        
        
        //设置自动布局,通过VFL
        //将约束添加到父视图上
        //自动布局需要手动屏蔽frame布局
//        circleIcon.translatesAutoresizingMaskIntoConstraints = false
//        self.addConstraint(NSLayoutConstraint(item: circleIcon, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        
        largeHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon.snp_centerX)
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.width.equalTo(225)
        }
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        backView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(registerBtn.snp_top).offset(-25)
        }
        
        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        
    }

    lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    lazy var largeHouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    lazy var tipLabel: UILabel = {
       
        let l = UILabel()
        
        l.text = "登录后,别人评论你的微薄别人评论你的微薄别人评论你的微"
        l.font = UIFont.systemFontOfSize(14)
        l.textAlignment = .Center
        l.numberOfLines = 0
        l.textColor = UIColor.darkGrayColor()
        
        l.sizeToFit()
        
        return l
    }()
    
    lazy var loginBtn: UIButton = {
       
        let btn = UIButton()
        
        btn.setTitle("登陆", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(16)
        
        let image = UIImage(named: "common_button_white_disable")!
        
        let w = Int (image.size.width/2)
        let h = Int (image.size.height/2)
        
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(w, topCapHeight: h), forState: UIControlState.Normal)
        
        return btn
    }()
    
    lazy var registerBtn: UIButton = {
       
        let btn = UIButton()
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(16)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        let image = UIImage(named: "common_button_white_disable")!
        
        let w = Int (image.size.width/2)
        let h = Int (image.size.height/2)
        
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(w, topCapHeight: h), forState: UIControlState.Normal)
        
        return btn
    }()
    
    
}
